@echo off
setlocal enabledelayedexpansion

REM MongoShake 定时任务管理脚本 (Windows版本)

set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "NC=[0m"

REM 打印带颜色的消息
:print_message
echo %GREEN%[INFO]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 检查Python是否安装
:check_python
python --version >nul 2>&1
if errorlevel 1 (
    call :print_error "Python is not installed or not in PATH"
    call :print_message "Please install Python 3.7+ and add it to PATH"
    exit /b 1
)
goto :eof

REM 安装依赖
:install_deps
call :print_message "Installing Python dependencies..."
pip install requests python-daemon >nul 2>&1
if errorlevel 1 (
    call :print_warning "Failed to install some dependencies, but continuing..."
)
goto :eof

REM 启动调度器
:start_scheduler
call :print_message "Starting MongoShake scheduler..."
if "%1"=="daemon" (
    call :print_message "Running in daemon mode..."
    start /B python scheduler.py --daemon
) else (
    call :print_message "Running in foreground mode..."
    python scheduler.py
)
goto :eof

REM 停止调度器
:stop_scheduler
call :print_message "Stopping MongoShake scheduler..."
taskkill /f /im python.exe /fi "WINDOWTITLE eq scheduler.py" >nul 2>&1
taskkill /f /im python.exe /fi "COMMANDLINE eq scheduler.py" >nul 2>&1
call :print_message "Scheduler stopped"
goto :eof

REM 查看调度器状态
:status_scheduler
call :print_message "Checking scheduler status..."
tasklist /fi "IMAGENAME eq python.exe" /fi "COMMANDLINE eq scheduler.py" 2>nul | findstr "python.exe" >nul
if not errorlevel 1 (
    call :print_message "Scheduler is running"
) else (
    call :print_message "Scheduler is not running"
)
goto :eof

REM 查看调度器日志
:logs_scheduler
call :print_message "Showing scheduler logs..."
if exist "mongoshake_scheduler.log" (
    type mongoshake_scheduler.log
) else (
    call :print_message "No log file found"
)
goto :eof

REM 立即执行同步
:sync_now
call :print_message "Executing sync now..."
python -c "
import sys
sys.path.append('.')
from scheduler import MongoShakeScheduler
scheduler = MongoShakeScheduler()
scheduler.perform_sync()
"
goto :eof

REM 创建Windows计划任务
:create_task
call :print_message "Creating Windows scheduled task..."
schtasks /create /tn "MongoShake Sync" /tr "%~dp0scheduler.bat run" /sc daily /st 00:00 /f >nul 2>&1
if errorlevel 1 (
    call :print_error "Failed to create scheduled task"
    call :print_message "Please run as administrator"
) else (
    call :print_message "Scheduled task created successfully"
)
goto :eof

REM 删除Windows计划任务
:delete_task
call :print_message "Deleting Windows scheduled task..."
schtasks /delete /tn "MongoShake Sync" /f >nul 2>&1
call :print_message "Scheduled task deleted"
goto :eof

REM 显示帮助信息
:show_help
echo Usage: %0 [COMMAND]
echo.
echo Commands:
echo   start       Start scheduler in foreground mode
echo   daemon      Start scheduler in daemon mode
echo   stop        Stop scheduler
echo   status      Show scheduler status
echo   logs        Show scheduler logs
echo   sync        Execute sync immediately
echo   install     Install Python dependencies
echo   task-create Create Windows scheduled task
echo   task-delete Delete Windows scheduled task
echo   help        Show this help message
echo.
echo Examples:
echo   %0 start                    # Start scheduler
echo   %0 daemon                   # Start scheduler in background
echo   %0 sync                     # Execute sync now
echo   %0 logs                     # Show logs
goto :eof

REM 主函数
:main
call :check_python
if errorlevel 1 exit /b 1

if "%1"=="" goto :show_help
if "%1"=="start" goto :start_scheduler
if "%1"=="daemon" goto :start_scheduler
if "%1"=="stop" goto :stop_scheduler
if "%1"=="status" goto :status_scheduler
if "%1"=="logs" goto :logs_scheduler
if "%1"=="sync" goto :sync_now
if "%1"=="install" goto :install_deps
if "%1"=="task-create" goto :create_task
if "%1"=="task-delete" goto :delete_task
if "%1"=="help" goto :show_help
if "%1"=="--help" goto :show_help
if "%1"=="-h" goto :show_help

call :print_error "Unknown command: %1"
call :show_help
exit /b 1

REM 执行主函数
call :main %* 